package cn.gov.cbrc.core.servlet;

import java.io.File;
import java.io.FilenameFilter;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

import cn.gov.cbrc.core.util.PropertiesUtil;

/**
 * 日志查询
 */
public class QueryLogList extends HttpServlet {
	private static final long serialVersionUID = 1L;
	/*每页日志大小*/
	//private static int pageSize = 50;
	
    public QueryLogList() {
        super();
    }

	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		this.doPost(request, response);
	}

	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		request.setCharacterEncoding("UTF-8");
		response.setContentType("text/html;charset=UTF-8");
		PrintWriter out = response.getWriter();
		String logPath = PropertiesUtil.getProperty("logPath");
		//页码
		int pageNo = Integer.parseInt(request.getParameter("page"));
		int pageSize = Integer.parseInt(request.getParameter("rows"));
		String[] logNames = null;
		JSONArray jsonArray = new JSONArray();
		JSONObject json = new JSONObject();
		File file = new File(logPath);
		String rowsStr = "";
		HttpSession session = request.getSession();
		List<String> logNameList = (List<String>) session.getAttribute("LOGNAME_LIST");
		if(logNameList==null){
			if(file.isDirectory()){
				logNames = file.list(new FilenameFilter() {
					@Override
					public boolean accept(File dir, String name) {
						if(name.toLowerCase().endsWith(".log")){return true;}else{return false;}
					}
				});
				logNameList = new ArrayList<String>();
				for(int i=0;i<logNames.length;i++){
					logNameList.add(logNames[i]);
				}
				session.setAttribute("LOGNAME_LIST", logNameList);
			}
		}
		int startNo = (pageNo-1)*pageSize;
		int endNo = pageNo*pageSize-1;
		if(endNo > logNameList.size()-1){
			endNo = logNameList.size() - 1;
		}
		for(int i=startNo;i<=endNo;i++){
			jsonArray.add("{\"logNames\":\""+logNameList.get(i)+"\"}");
		}
		
//		if(file.isDirectory()){
//			logNames = file.list(new FilenameFilter() {
//				@Override
//				public boolean accept(File dir, String name) {
//					if(name.toLowerCase().endsWith(".log")){return true;}else{return false;}
//				}
//			});
//			rowsStr = "{\"total\":"+logNames.length+",\"rows\":[";
//			for(String str:logNames){//将数组转化为jsonArray
//				jsonArray.add(str);
//				rowsStr += "{\"logNames\":\""+str+"\"},";
//			}
//			rowsStr = rowsStr.substring(0, rowsStr.length()-1);
//			rowsStr += "]}";
//		}
		System.out.println(rowsStr);
		json.put("total", logNameList.size());
		json.put("rows",jsonArray);
		out.print(json);
		out.flush();
		out.close();
	}

}
